package com.wangyh.edit.prettyedit.help;

import com.wangyh.edit.prettyedit.bean.PropertyCell;
import com.wangyh.edit.prettyedit.constant.EditConstant;
import com.wangyh.edit.prettyedit.pages.IPageService;
import com.wangyh.edit.prettyedit.util.PreferencesUtils;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;

import javax.security.auth.callback.Callback;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户属性列表
 * @author wangyh
 *
 */
public class PropertyPage implements IPageService {

    @Override
    public Node generatePageToRoot(Pane root) {
        return root;
    }

    @Override
    public Node generatePage(String[] args) {
        //构造数据
        ObservableList<PropertyCell> data = FXCollections.observableArrayList();
        //用户列表
        String[] keys = PreferencesUtils.getPreferenceKeys(EditConstant.storeUser);
        int index = 0;
        for(int i=0; i<keys.length; i++){
            PropertyCell propertyCell = new PropertyCell(keys[i],
                    PreferencesUtils.getPreferences(EditConstant.storeUser,keys[i],""));
            propertyCell.setIndex(index+1);
            propertyCell.setType("用户");
            data.add(propertyCell);
            index ++ ;
        }

        //配置列表
        keys = PreferencesUtils.getPreferenceKeys(EditConstant.storeConfig);
        for(int i=0; i<keys.length; i++){
            PropertyCell propertyCell = new PropertyCell(keys[i],
                    PreferencesUtils.getPreferences(EditConstant.storeConfig,keys[i],""));
            propertyCell.setIndex(index + 1);
            propertyCell.setType("配置");
            data.add(propertyCell);
            index ++ ;
        }

        //在写的时候也可以先初始化然后再设置数据源
        TableView<PropertyCell> table = new TableView<>();
        TableColumn<PropertyCell, String> indexCol = new TableColumn<>("序号");
        TableColumn<PropertyCell, String> typeCol = new TableColumn<>("类型");
        TableColumn<PropertyCell, String> keyCol = new TableColumn<>("键");
        TableColumn<PropertyCell, String> valueCol = new TableColumn<>("值");
        indexCol.setMinWidth(50);
        typeCol.setMinWidth(100);
        keyCol.setMinWidth(140);
        valueCol.setMinWidth(340);

        //设置居中
        indexCol.setStyle( "-fx-alignment: CENTER;");
        typeCol.setStyle( "-fx-alignment: CENTER;");

        indexCol.setCellValueFactory(new PropertyValueFactory<>("index"));
        typeCol.setCellValueFactory(new PropertyValueFactory<>("type"));
        keyCol.setCellValueFactory(new PropertyValueFactory<>("key"));
        valueCol.setCellValueFactory(new PropertyValueFactory<>("value"));



        table.getColumns().addAll(indexCol, typeCol, keyCol, valueCol);

        table.setItems(data);
        return table;
    }


}
